package com.ruoyi.supermarket.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.Supply;
import com.ruoyi.supermarket.service.ISupplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供客Controller
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@RestController
@RequestMapping("/supermarket/supply")
public class SupplyController extends BaseController
{
    @Autowired
    private ISupplyService supplyService;

    /**
     * 查询供客列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:list')")
    @GetMapping("/list")
    public TableDataInfo list(Supply supply)
    {
        startPage();
        List<Supply> list = supplyService.selectSupplyList(supply);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('supermarket:supply:list')")
    @GetMapping("/listOne")
    public TableDataInfo listOne(Supply supply)
    {
        startPage();
        List<Supply> list = supplyService.selectSupplyListOne(supply);
        return getDataTable(list);
    }

    /**
     * 查询供应商列表
     */
//    @PreAuthorize("@ss.hasPermi('supermarket:supply:list')")
    @GetMapping("/gkList/{type}")
    public AjaxResult gkList(@PathVariable("type") Long type)
    {
        List<Supply> list = supplyService.selectGKSupplyList(type);
        return success(list);
    }

    /**
     * 导出供客列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:export')")
    @Log(title = "供客", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supply supply)
    {
        List<Supply> list = supplyService.selectSupplyList(supply);
        ExcelUtil<Supply> util = new ExcelUtil<Supply>(Supply.class);
        util.exportExcel(response, list, "供客数据");
    }

    /**
     * 获取供客详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:query')")
    @GetMapping(value = "/{supplyId}")
    public AjaxResult getInfo(@PathVariable("supplyId") Long supplyId)
    {
        return success(supplyService.selectSupplyBySupplyId(supplyId));
    }

    /**
     * 新增供客
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:add')")
    @Log(title = "供客", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supply supply)
    {
        if (!supplyService.findSupplyByName(supply))
        {
            return error("新增供客'" + supply.getSupplyName() + "'失败，供客名称已存在");
        }
        supplyService.insertSupply(supply);
        return success();
    }

    /**
     * 修改供客
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:edit')")
    @Log(title = "供客", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supply supply)
    {
        if(!supplyService.findSupplyByName(supply)){
            return error("修改供客："+supply.getSupplyName()+"名称已存在，请重新输入！");
        }
        return toAjax(supplyService.updateSupply(supply));
    }

    /**
     * 删除供客
     */
    @PreAuthorize("@ss.hasPermi('supermarket:supply:remove')")
    @Log(title = "供客", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplyIds}")
    public AjaxResult remove(@PathVariable Long[] supplyIds)
    {
        return toAjax(supplyService.deleteSupplyBySupplyIds(supplyIds));
    }

    @PreAuthorize("@ss.hasPermi('supermarket:supply:sh')")
    @Log(title = "供客", businessType = BusinessType.UPDATE)
    @PutMapping("/sh")
    public AjaxResult sh(@RequestBody Supply supply)
    {
        return supplyService.shSupply(supply);
    }


    @PreAuthorize("@ss.hasPermi('supermarket:supply:qj')")
    @Log(title = "供客", businessType = BusinessType.UPDATE)
    @GetMapping("/status/{supplyId}")
    public AjaxResult status(@PathVariable Long supplyId)
    {
        return supplyService.status(supplyId);
    }

}
